import { defineStore } from "pinia";
import { UserState } from "@/stores/interface";
import { getUserInfoApi, logoutApi } from "@/api/login";
import { removeToken } from "@/utils/auth";
export const useUserStore = defineStore({
  id: "geeker-user",
  state: (): UserState => ({
    token: "",
    avatar: new URL("/src/assets/images/avatar.gif", import.meta.url).href,
    userInfo: {},
    roles: [],
    permissions: []
  }),
  getters: {},
  actions: {
    // Set Avatar
    setAvatar(avatar: UserState["avatar"]) {
      this.avatar = avatar;
    },
    // Set setUserInfo
    setUserInfo(userInfo: UserState["userInfo"]) {
      this.userInfo = userInfo;
    },
    async getUserInfo() {
      const { data } = await getUserInfoApi();
      this.userInfo = { ...data, name: data.user.nickName };
      this.roles = data.roles;
      this.permissions = data.permissions;
      this.setAvatar(data.user.avatar);
    },
    async logout() {
      await logoutApi();
      this.token = "";
      this.roles = [];
      this.permissions = [];
      removeToken();
    }
  }
  // persist: piniaPersistConfig('geeker-user')
});
